﻿using System;

namespace RigLib
{
    class Hex2Bin
    {
        public void HexToBin(string[] args)
    {
                    const string header = "****Hex to bin. convert with leading zeros. 'q' to quit****\n\n";
            const string prefix = "0x";
            const string prompt = " -> ";
            const string badFormatErr = "Enter a hex value!\n";
            string ret;

            /** fancy stuff ***/
            Console.BackgroundColor = ConsoleColor.Blue;
            Console.WriteLine(header);
            Console.BackgroundColor = ConsoleColor.Black;

            /** command line args **/
            if (args.Length > 0)
            {
                ret = Convert.ToString(Convert.ToInt32(args[0], 16), 2);
                Console.WriteLine(prefix + args[0].ToString().ToUpper() + prompt + ret.PadLeft(8, '0'));
            }

            Console.Write(prompt);
            string cin = String.Format("{0:X}", Console.ReadLine());

            while (cin != "q")
            {
                try
                {
                    ret = Convert.ToString(Convert.ToInt32(cin, 16), 2);
                    Console.WriteLine(prefix + cin.ToUpper() + prompt + ret.PadLeft(8, '0'));
                }
                catch (FormatException)
                {
                    Console.WriteLine(badFormatErr);
                }

                Console.Write(prompt);
                cin = String.Format("{0:X}", Console.ReadLine());

            }
        }
    
    }
}
